//
//  WJVideoView.m
//  WJBSApp
//
//  Created by dllo on 16/7/21.
//  Copyright © 2016年 王景伟. All rights reserved.
//
// 需要加次数和时间 还有播放的东西


#import "WJVideoView.h"
#import "WJEssAndNewModel.h"
#import "DALabeledCircularProgressView.h"
#import <AVFoundation/AVFoundation.h>
#import "WJDataManager.h"
@interface WJVideoView ()

// video 和 audio 的三个东西

@property (nonatomic,strong) UILabel *playLable;

@property (nonatomic,strong) UILabel *durationLable;

@property (nonatomic,strong) DALabeledCircularProgressView *progressView;



@end


@implementation WJVideoView

-(void)dealloc
{
    [_model release];
    [_middenImage release];
    [_playLable release];
    [_durationLable release];
    [_progressView release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    [self createView];
    return self;
}


- (void)createView
{
    self.userInteractionEnabled = YES;

    
    // 这个是圆圈圈
    self.progressView = [[DALabeledCircularProgressView alloc] init];
    self.progressView.roundedCorners = YES; // 头变成圆的
    [self addSubview:self.progressView];
    self.progressView.hidden = YES;
    self.progressView.progressTintColor = BSOrangeColor;
    [_progressView release];
    
    self.middenImage = [[UIImageView alloc]init];
    self.middenImage.backgroundColor = BSClearColor;
    self.middenImage.image = [UIImage imageNamed:@"video-play"];
    [self addSubview:self.middenImage];
    [_middenImage release];

    self.playLable = [[UILabel alloc]init];
    self.playLable.backgroundColor = [UIColor colorWithRed:29/255.0 green:29/255.0 blue:29/255.0 alpha:0.8];
    self.playLable.font = [UIFont systemFontOfSize:13];
    self.playLable.textColor = BSWhiteColor;
    [self addSubview:self.playLable];
    [_playLable release];

    self.durationLable = [[UILabel alloc]init];
    self.durationLable.backgroundColor = [UIColor colorWithRed:29/255.0 green:29/255.0 blue:29/255.0 alpha:0.8];
    self.durationLable.font = [UIFont systemFontOfSize:13];
    self.durationLable.textColor = BSWhiteColor;
    [self addSubview:self.durationLable];
    [_durationLable release];
    
      /** 给视图添加手势  添加手指 */
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapPlayAction:)];
    [self.middenImage addGestureRecognizer:tap];
    self.middenImage.userInteractionEnabled = YES;
    [tap release];
}

#pragma mark - 负责 赋值的方法
- (void)setModel:(WJEssAndNewModel *)model
{
    if (_model !=model) {
        [_model release];
        _model = [model retain];
    }
    
 
    //设置时间格式
    
    NSInteger min = model.videoModel.duration / 60;
    
    NSInteger sec = model.videoModel.duration % 60;
    
    self.durationLable.text = [NSString stringWithFormat:@"%02zd:%02zd",min,sec];
    
    self.playLable.text = [NSString stringWithFormat:@"%ld次播放",(long)model.videoModel.playcount];
    

    
    [self sd_setImageWithURL:[NSURL URLWithString:self.model.videoModel.thumbnail[0]] placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
        if ([[WJDataManager shareFMDB] isExistPicName:self.model.videoModel.thumbnail[0]]) {
            
            self.progressView.hidden = YES;
            
        }else{

        self.progressView.hidden = NO;
        
        self.middenImage.hidden = YES;
        
        self.playLable.hidden = YES;
        
        self.durationLable.hidden = YES;

        CGFloat proges = 1.0 * receivedSize / expectedSize;
        
        // 显示文字
        NSString *text = [NSString stringWithFormat:@"%.0f%%", proges * 100];
        self.progressView.progressLabel.text = [text stringByReplacingOccurrencesOfString:@"-" withString:@""];
        
        //显示进度值
        [self.progressView setProgress:proges animated:NO];
        }
        
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        if (![[WJDataManager shareFMDB] isExistPicName:self.model.videoModel.thumbnail[0]]) {
            
            [[WJDataManager shareFMDB] insertPicName:self.model.videoModel.thumbnail[0]];
            
        }

        self.progressView.hidden = YES;
        
        self.middenImage.hidden = NO;
        
        self.playLable.hidden = NO;
        
        self.durationLable.hidden = NO;
    }];
    
    
}

-(void)layoutSubviews {
    [super layoutSubviews];
    
    NSInteger newHeight = self.model.videoModel.height * (WIDTH - 20) / self.model.videoModel.width;
    
    NSDictionary *namedic = @{NSFontAttributeName:[UIFont systemFontOfSize:13]};
    CGRect nameRect = [self.playLable.text boundingRectWithSize:CGSizeMake(1000, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:namedic context:nil];
    self.playLable.frame = CGRectMake(0, newHeight - 20, nameRect.size.width, 20);
    
    
    NSDictionary *timedic = @{NSFontAttributeName:[UIFont systemFontOfSize:13]};
    CGRect timeRect = [self.durationLable.text boundingRectWithSize:CGSizeMake(1000, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:timedic context:nil];
    self.durationLable.frame = CGRectMake(WIDTH - 20 - timeRect.size.width, newHeight - 20, timeRect.size.width, 20);
    
    self.middenImage.frame = CGRectMake(self.bounds.size.width /2 - 40, self.bounds.size.height /2 - 40, 80, 80);

    self.progressView.frame = CGRectMake(self.bounds.size.width / 2 - 30, self.bounds.size.height / 2 - 30, 60, 60);

}



#pragma mark - 点击可以进行播放
- (void)tapPlayAction:(UITapGestureRecognizer *)tap {
    
    NSLog(@"点击了视屏");
    
    WJAVPlayView *playerView = [WJAVPlayView sharedPlayView];
    playerView.frame = self.bounds;
    [self addSubview:playerView];
    
    playerView.playerUrl = [NSURL URLWithString:self.model.videoModel.video[1]];
    
    [playerView play];
    
    NSString *str = [NSString stringWithFormat:@"%ld",tap.view.tag];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"点击了视屏" object:str];
}





















@end
